from django.shortcuts import render
from .models import *
# Create your views here.


def indexview(request):
    try:
        # 热搜歌曲
        search_song = Dynamic.objects.select_related('song').order_by('-dynamic_search').all()[:12]
        # 音乐分类
        label_list = Label.objects.all()
        # 热门歌曲
        play_hot_song = Dynamic.objects.select_related('song').order_by('-dynamic_plays').all()[10:20]
        # 新歌推荐
        daily_recommendation = Song.objects.order_by('-song_release').all()[:3]
        # 热门搜索、热门下载
        search_ranking = search_song[:12]
        down_ranking = Dynamic.objects.select_related('song').order_by('-dynamic_down').all()[:12]
        all_ranking = [search_ranking, down_ranking]
        return render(request, 'index.html', locals())
    except Exception as e:
        print(f"Error in indexview: {str(e)}")  # 添加日志输出
        return render(request, 'error404.html', status=500)


# 500
def page_error(request):
    return render(request, 'error404.html', status=500)

# 404
def page_not_found(request, exception):
    return render(request, 'error404.html', status=404)
